//
//  AddressController.m
//  jang
//
//  Created by lee han on 23/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "AddressController.h"
#import "CommonView.h"
#import "CRUserData.h"

@interface AddressController ()

@end

@implementation AddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.navigationItem.backBarButtonItem setTitle:@""];
    
    if(_mType == 1){
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一步" style:UIBarButtonItemStyleDone target:self action:@selector(onClickNext:)];
        
        self.navigationItem.rightBarButtonItem = barButtonItem;
    }
    else{
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"+新增" style:UIBarButtonItemStyleDone target:self action:@selector(onClickAdd:)];
        
        self.navigationItem.rightBarButtonItem = barButtonItem;
    }
    
    [_mTableView registerNib:[UINib nibWithNibName:@"AddressCell" bundle:nil] forCellReuseIdentifier:@"AddressCell"];
    _mTableView.delegate = self;
    _mTableView.dataSource = self;
    
    
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

-(void)viewWillAppear:(BOOL)animated{
    if(self.tabBarController.tabBar.hidden == YES)
        self.tabBarController.tabBar.hidden = NO;
    [_mTableView reloadData];
    [super viewWillAppear:animated];
}


- (void)onClickAdd:(id)sender{
    self.tabBarController.tabBar.hidden = YES;
    [self performSegueWithIdentifier:@"toaddaddress" sender:self];
}

- (void)onClickNext:(id)sender{
    self.tabBarController.tabBar.hidden = YES;
    [CRUserData sharedInstance].mOrderAddressID = mSelectAddressID;
    [self performSegueWithIdentifier:@"toorder" sender:self];
}

- (void)requestData{
    NSMutableDictionary* data = [[NSMutableDictionary alloc] init];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_AddressList];
}

-(void)setShopcarType{
    _mType = 1;
    mSelectAddressID = [[CRUserData sharedInstance] getDefaultAddressID];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark UITableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
//end UITableViewDelegate

#pragma mark UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [[CRUserData sharedInstance] getAddressCount];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressCell *cell = [_mTableView dequeueReusableCellWithIdentifier:@"AddressCell" forIndexPath:indexPath];
    
    NSDictionary* data = [[CRUserData sharedInstance] getAddressData: indexPath.section];
    [cell initWith:data type:_mType delegate:self];
    if(_mType == 1){
        [cell setCellSelected:(mSelectAddressID == [[data objectForKey:@"id"] integerValue])];
    }
    return cell;
}

//end UITableViewDataSource


#pragma mark networkDelegate
- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if (type == NetworkFunctionType_AddressList){
        if([[response objectForKey:@"status"] integerValue] == 1){
            
            [[CRUserData sharedInstance] setAddressData:[response objectForKey:@"data"]];
            mSelectAddressID = [[CRUserData sharedInstance] getDefaultAddressID];
            [_mTableView reloadData];
        }
        else{
            [CommonView addAlertView:self Text:@"收货地址错误"];
        }
    }
}
//end networkDelegate

#pragma mark AddressCellDelegate
-(void)onAddressCellSelected:(NSInteger)addressid{
    if (mSelectAddressID == addressid) {
        return;
    }
    mSelectAddressID = addressid;
    [_mTableView reloadData];
}

- (void)onAddressCellChanged{
    [_mTableView reloadData];
}
//end AddressCellDelegate

@end
